import org.h2.jdbcx.JdbcDataSource;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

@SuppressWarnings("Duplicates")
@WebServlet("/jdbc3")
public class Servlet extends HttpServlet {

    protected void doGet(HttpServletRequest req, HttpServletResponse resp){

        try (Connection conn = (Connection) getServletContext().getAttribute("connection")) {
                Statement stmt = conn.createStatement();
                stmt.executeUpdate(
                        "CREATE TABLE IF NOT EXISTS `SOCRE` (" +
                                "  `id` varchar(100) NOT NULL ," +
                                "  `course` varchar(100) NOT NULL," +
                                "  `score` int(11) NOT NULL," +
                                "  PRIMARY KEY (`id`)" +
                                ");"
                );
                stmt.executeUpdate(
                        "insert into `SOCRE` (`id`,`course`,`score`) values ('201741412101', '计算机', 88);" +
                                "insert into `SOCRE` (`id`,`course`,`score`) values ('201741412125', 'C语言', 95);"+
                                    "insert into `SOCRE` (`id`,`course`,`score`) values ('201741412129', 'Java', 90);"
                );
                 ResultSet rs = stmt.executeQuery("select * from SOCRE");//选择import java.sql.ResultSet;


            // 遍历每行记录
            System.out.println("学生成绩表如下：");
            while (rs.next()) {
                //如果结果集中有数据，就会循环打印出来
                System.out.println(rs.getString("id") + "," + rs.getString("course") + "," + rs.getInt("score"));
            }
        }
        catch (SQLException e) {
            e.printStackTrace();
    }
    }
}
